import org.kwis.msp.lcdui.Graphics;

public class Block {
    public static final int NOCELL = 0;
    private int currentX, currentY;
    private int cell[][];
    private int type;
    public static final int colorTable[][] = {{204, 255, 102},
                                              {255,   0, 153},
                                              {255, 255,  51},
                                              {  0, 102, 255},
                                              {  0, 204, 153},
                                              { 51, 153,  51},
                                              { 51, 255, 204}};
    public Block(int type) {
        currentX = 4;
        currentY = 0;
        cell = new int[4][4];
        this.type = type;

        for(int i = 0; i < 4; i++)
            for(int j = 0; j < 4; j++)
                cell[i][j] = NOCELL;

		switch(type) {
            case 1 : int temp1[][] = {{0, 0, 0, 0},
                                     {0, 0, 0, 0},
                                     {1, 1, 1, 1},
                                     {0, 0, 0, 0}};
                     setCell(temp1);
                     break;
            case 2 : int temp2[][] = {{0, 0, 0, 0},
                                     {2, 0, 0, 0},
                                     {2, 2, 2, 0},
                                     {0, 0, 0, 0}};
                     setCell(temp2);
                     break;
            case 3 : int temp3[][] = {{0, 0, 0, 0},
                                     {0, 0, 3, 0},
                                     {3, 3, 3, 0},
                                     {0, 0, 0, 0}};
                     setCell(temp3);
                     break;
            case 4 : int temp4[][] = {{0, 0, 0, 0},
                                     {4, 4, 0, 0},
                                     {0, 4, 4, 0},
                                     {0, 0, 0, 0}};
                     setCell(temp4);
                     break;
            case 5 : int temp5[][] = {{0, 0, 0, 0},
                                     {0, 5, 5, 0},
                                     {5, 5, 0, 0},
                                     {0, 0, 0, 0}};
                     setCell(temp5);
                     break;
            case 6 : int temp6[][] = {{0, 0, 0, 0},
                                     {0, 6, 6, 0},
                                     {0, 6, 6, 0},
                                     {0, 0, 0, 0}};
                     setCell(temp6);
                     break;
            case 7 : int temp7[][] = {{0, 0, 0, 0},
                                     {0, 7, 0, 0},
                                     {7, 7, 7, 0},
                                     {0, 0, 0, 0}};
                     setCell(temp7);
                     break;
        }
    }

    
	
	public void setCell(int c[][]) {
        for(int i = 0; i < 4; i++)
            for(int j = 0; j < 4; j++)
                cell[i][j] = c[i][j];
    }

    public void move(int x, int y) {
        currentX += x;
        currentY += y;
    }
	
	public boolean moveOk(BackBoard b) {
        for(int i = 0; i < 4; i++)
            for(int j = 0; j < 4; j++)
                if(cell[i][j] != NOCELL) {
                    if((i + currentX < 0) || (j + currentY < 0) ||
                       (i + currentX >= BackBoard.MAXWIDTH) || (j + currentY >= BackBoard.MAXHEIGHT))
                        return false;
                    if((b.getBoard())[i+currentX][j+currentY] != NOCELL)
                        return false;
                }
        return true;
    }

    public void rotate() {
        int temp = cell[3][0];
        cell[3][0] = cell[3][3];
        cell[3][3] = cell[0][3];
        cell[0][0] = cell[0][0];
        cell[0][0] = temp;
        temp = cell[2][0];
        cell[2][0] = cell[3][2];
        cell[3][2] = cell[1][3];
        cell[1][3] = cell[0][1];
        cell[0][1] = temp;
        temp = cell[3][1];
        cell[3][1] = cell[2][3];
        cell[2][3] = cell[0][2];
        cell[0][2] = cell[1][0];
        cell[1][0] = temp;
        temp = cell[2][1];
        cell[2][1] = cell[2][2];
        cell[2][2] = cell[1][2];
        cell[1][2] = cell[1][1];
        cell[1][1] = temp;
    }

	public void drawBlock(Graphics g) {
        for(int i = 0; i < 4; i++)
            for(int j = 0; j < 4; j++)
                if (cell[i][j] != NOCELL)
                   drawCell(g, type, (i + currentX) * 6 + 1, (j + currentY) * 6 + 10);
    }

    public void drawCell(Graphics g, int type, int x, int y) {
        g.setColor(colorTable[type - 1][0], colorTable[type - 1][1], colorTable[type - 1][2]);
        g.fillRect(x, y, 5, 5);
    }

    public void freeze(BackBoard b) {
        try {
            for(int i = 0; i < 4; i++)
                for(int j = 0; j < 4; j++)
                    if(cell[i][j] != NOCELL)
                        (b.getBoard())[i + currentX][j + currentY] = cell[i][j];
        } catch(Exception e) { System.out.println(e.getMessage()); }
    }

	public int getType() {
        return this.type;
    }

    public int[][] getCell() {
        return this.cell;
    }
}

